package com.asset.modules.system.controller;

import com.asset.modules.common.model.ResponseBean;
import com.asset.modules.common.utils.AesCipherUtil;
import com.asset.modules.common.utils.Constant;
import com.asset.modules.common.utils.StringUtil;
import com.asset.modules.system.model.User;
import com.asset.modules.system.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
@Api(tags = "用户信息接口")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserServiceImpl userService;

    @ApiOperation(value = "用户的保存和更新",notes = "在用户更新时，不需要提交companyCode、companyName、roleType、roleName等")
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    @NotNull
    public ResponseBean saveOrUpdate(@RequestBody User user){
        if(StringUtil.isBlank(user.getUserName())||StringUtil.isBlank(user.getPassword())){
            return new ResponseBean(HttpStatus.OK.value(), "密码为空！", false);
        }
        user.setPassword(AesCipherUtil.enCrypto(user.getUserName()+user.getPassword()));
        try{
            userService.saveOrUpdate(user);
            return new ResponseBean(HttpStatus.OK.value(), Constant.SUCCESS_REQUEST, true);
        }catch (Exception e){
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "信息保存失败，请查看是否有重复信息!", false);
        }
    }

    @ApiOperation(value = "根据ID获取用户信息")
    @RequestMapping(value = "/byId", method = RequestMethod.GET)
    @ResponseBody
    @NotNull
    public ResponseBean getUserById(@RequestParam String id){
        User user = userService.getUserById(id);
        String info ="请求成功！";
        if(user==null){
            info = "不存在此用户！";
        }
        return new ResponseBean(HttpStatus.OK.value(), info, user);
    }


    @ApiOperation(value = "获取所有用户")
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean getUsers(){
        return new ResponseBean(HttpStatus.OK.value(), "请求成功!", userService.getAll());
    }

    @ApiOperation(value = "删除用户")
    @RequestMapping(value = "/remove", method = RequestMethod.GET)
    @ResponseBody
    @NotNull
    public ResponseBean removeUsers(@RequestParam String ids){
        String msg = "删除用户信息";
        if(StringUtil.isBlank(ids)){
            msg  = "待删除用户ID为空";
        }
        String[] removeIds = ids.split(",");
        List list = new ArrayList();
        for(String id : removeIds){
            list.add(id);
        }
        return new ResponseBean(HttpStatus.OK.value(), msg, userService.removeByIds(list));
    }


    @ApiOperation(value = "根据手机号码获取用户信息")
    @RequestMapping(value = "/getByMobile", method = RequestMethod.GET)
    @ResponseBody
    @NotNull
    public ResponseBean getUserByMobile(@RequestParam String mobile){
        return new ResponseBean(HttpStatus.OK.value(), "请求成功！", userService.getUser("mobile", mobile)
        );
    }

}
